/**
 *  Copyright (c) 2010 The edtf project.  All rights reserved.
 *
 *  This file is part of edtf (Event Driven Testing Framework).
 *
 *  edtf is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  edtf is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with edtf.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  This software consists of voluntary contributions made by many
 *  individuals.  For exact contribution history, see the revision
 *  history and logs, available at http://code.google.com/p/edtf.
 */
package org.xepf.xml.saxon;

import java.util.Date;
import net.sf.saxon.expr.PJConverter;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.type.ConversionResult;
import net.sf.saxon.type.ValidationFailure;
import net.sf.saxon.value.DayTimeDurationValue;
import org.xepf.xml.adapter.XMLTypeConvertor;
import org.xepf.xml.adapter.XMLNode;

/**
 *
 *
 */
public class XMLTypeConvertorImp implements XMLTypeConvertor {

    @Override
    public Date xsdDateToJavaDate(XMLNode xmlNode) throws Exception {
        NodeInfo xsdDateNode = (NodeInfo) xmlNode.getNativeNode();
        PJConverter dateConverter = new PJConverter.CalendarValueToDate();
        XPathContext context = xsdDateNode.getConfiguration().getConversionContext();
        Date date = (Date) dateConverter.convert(xsdDateNode, Date.class, context);
        return date;
    }

    @Override
    public XMLNode javaDateToXsdDate(Date date) throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public long xsdDayTimeDurationISO8601ToMilliseconds(String xsdDayTimeDuration) throws Exception {
        ConversionResult result = DayTimeDurationValue.makeDayTimeDurationValue(xsdDayTimeDuration);
        if (result instanceof ValidationFailure) {
            ValidationFailure validationFailure = (ValidationFailure) result;
            throw new Exception("Failed to convert xsd:dayTimeDuration string '" + xsdDayTimeDuration +
                    "' to milliseconds, because: " + validationFailure.getMessage());
        }

        DayTimeDurationValue dayTimeDuration = (DayTimeDurationValue) result;
        long durationInMilliseconds = dayTimeDuration.getLengthInMilliseconds();
        if (durationInMilliseconds < 0) {
            throw new Exception("The specified xsd:DayTimeDuration '" + xsdDayTimeDuration + 
                    "' exceeds Long.MAX_VALUE milliseconds, which is almost 300 million years. Please provide a smaller duration.");
        }

        return durationInMilliseconds;
    }

    @Override
    public String millisecondsToXsdDayTimeDurationISO8601(long durationInMilliseconds) throws Exception {
        DayTimeDurationValue dayTimeDuration = DayTimeDurationValue.fromMilliseconds(durationInMilliseconds);
        return dayTimeDuration.getStringValue();
    }
}
